import java.util.*;

public class Main {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = new int[]{-4,-2,-2,-2,0,1,2,2,2,3,3,4,4,6,6};
        System.out.println(solution.threeSum(nums));
    }
}
class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> ret = new ArrayList<>();
        for (int i = 0;i < nums.length-1;i++) {
            int fix = nums[i];
            int left = i+1;
            int right = nums.length-1;
            while (left<right) {
                if (nums[left]+nums[right]>-fix) {
                    right--;
                } else if (nums[left]+nums[right]<-fix) {
                    left++;
                } else {
                    List<Integer> list = new ArrayList<>();
                    if (nums[i]==nums[i+1]) {
                        i++;
                    }
                    if (nums[left]==nums[left+1]) {
                        left++;
                    } else if (nums[right]==nums[right-1]) {
                        right--;
                    }
                    list.add(nums[i]);
                    list.add(nums[left++]);
                    list.add(nums[right--]);
                    ret.add(list);
                }
            }
        }
        return ret;
    }
}